package top.tangyh.lamp.care.mapper;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Param;
import top.tangyh.basic.base.mapper.SuperMapper;
import top.tangyh.lamp.care.entity.BHl7YzYaoPlan;
import org.springframework.stereotype.Repository;
import top.tangyh.lamp.care.vo.query.YzYaoPlanQueryVO;
import top.tangyh.lamp.care.vo.result.CountExeYaoDetailTimeRangeBO;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static top.tangyh.lamp.common.constant.CommonConstant.NO_NUMBER;
import static top.tangyh.lamp.common.constant.CommonConstant.YES_NUMBER;

/**
 * <p>
 * Mapper 接口
 * 医嘱执行计划表
 * </p>
 *
 * @author zqb
 * @date 2024-07-22 15:09:13
 * @create [2024-07-22 15:09:13] [zqb] [代码生成器生成]
 */
@Repository
public interface BHl7YzYaoPlanMapper extends SuperMapper<BHl7YzYaoPlan> {


    /**
     * 根据条件查找列表
     *
     * @param queryVO
     * @return
     */
    default List<BHl7YzYaoPlan> selectListByQuery(YzYaoPlanQueryVO queryVO) {
        String patientIdent = queryVO.getPatientIdent();
        String hospitalNumber = queryVO.getHospitalNumber();
        String orderId = queryVO.getOrderId();
        LambdaQueryWrapper<BHl7YzYaoPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(orderId), BHl7YzYaoPlan::getOrderId, orderId)
                .eq(StrUtil.isNotBlank(hospitalNumber), BHl7YzYaoPlan::getHospitalNumber, hospitalNumber)
                .eq(StrUtil.isNotBlank(patientIdent), BHl7YzYaoPlan::getPatientIdent, patientIdent)
                .eq(BHl7YzYaoPlan::getIsDeleted, NO_NUMBER);
        return this.selectList(queryWrapper);
    }

   

    /**
     * 根据时间查询所有已执行执行用药主要或者出入量
     * @param hospitalNumber
     * @param medicationName
     * @param routeIdent
     * @param conditionIdent
     * @param startDate
     * @param endDate
     * @param inputType
     * @param iscare
     * @param statucode
     * @param isIo
     * @return
     */
    default List<BHl7YzYaoPlan> selectListByExeTimeQuery(String hospitalNumber, String medicationName, String routeIdent, String conditionIdent
            , String startDate, String endDate,Integer inputType,Integer iscare,String statucode,Integer isIo) {
        LambdaQueryWrapper<BHl7YzYaoPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(medicationName), BHl7YzYaoPlan::getMedication, medicationName)
                .eq(StrUtil.isNotBlank(hospitalNumber), BHl7YzYaoPlan::getHospitalNumber, hospitalNumber)
                .eq(StrUtil.isNotBlank(conditionIdent), BHl7YzYaoPlan::getConditionIdent, conditionIdent)
                .eq(StrUtil.isNotBlank(routeIdent), BHl7YzYaoPlan::getRouteIdent, routeIdent)
                .isNotNull(BHl7YzYaoPlan::getExeTime)
                .ge(StrUtil.isNotBlank(startDate), BHl7YzYaoPlan::getExeTime, startDate)
                .le(StrUtil.isNotBlank(endDate), BHl7YzYaoPlan::getExeTime, endDate)
                .eq(StrUtil.isNotBlank(statucode),BHl7YzYaoPlan::getExeStatus,statucode)
                .eq(null != inputType,BHl7YzYaoPlan::getInputType,inputType)
                .eq(null != iscare,BHl7YzYaoPlan::getIsShowCare,iscare)
                .eq(BHl7YzYaoPlan::getIsFromio,isIo)
                .eq(BHl7YzYaoPlan::getIsMaster,YES_NUMBER)
                .eq(BHl7YzYaoPlan::getIsDeleted, NO_NUMBER)
                .orderByAsc(BHl7YzYaoPlan::getPlanTime);
        if(null !=isIo && isIo == NO_NUMBER){
            queryWrapper.isNotNull(BHl7YzYaoPlan::getPlanTime);
        }
        return this.selectList(queryWrapper);
    }

    default List<BHl7YzYaoPlan> selectListByExeTimeQuery(String hospitalNumber, String medicationName, String routeIdent, String conditionIdent, String startDate, String endDate,Integer inputType) {
        LambdaQueryWrapper<BHl7YzYaoPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(medicationName), BHl7YzYaoPlan::getMedication, medicationName)
                .eq(StrUtil.isNotBlank(hospitalNumber), BHl7YzYaoPlan::getHospitalNumber, hospitalNumber)
                .eq(StrUtil.isNotBlank(conditionIdent), BHl7YzYaoPlan::getConditionIdent, conditionIdent)
                .eq(StrUtil.isNotBlank(routeIdent), BHl7YzYaoPlan::getRouteIdent, routeIdent)
                .isNotNull(BHl7YzYaoPlan::getPlanTime)
                .and(wapper->{
                    wapper.or(wapper1->{
                        wapper1.isNotNull(BHl7YzYaoPlan::getExeTime)
                                .ge(StrUtil.isNotBlank(startDate), BHl7YzYaoPlan::getExeTime, startDate)
                                .le(StrUtil.isNotBlank(endDate), BHl7YzYaoPlan::getExeTime, endDate);
                    });
                    wapper.or(wapper2->{
                        wapper2.ge(StrUtil.isNotBlank(startDate), BHl7YzYaoPlan::getPlanTime, startDate)
                                .le(StrUtil.isNotBlank(endDate), BHl7YzYaoPlan::getPlanTime, endDate);
                    });
                })
                .eq(BHl7YzYaoPlan::getIsMaster,YES_NUMBER)
                .eq(BHl7YzYaoPlan::getIsFromio,NO_NUMBER)
                .eq(null != inputType,BHl7YzYaoPlan::getInputType,inputType)
                //NW代表是新开的 CA代表取消 OC代表冲销
//                .eq( BHl7YzYaoPlan::getPlanStatusIdent, "NW")
                .eq( BHl7YzYaoPlan::getPlanStatusIdent, "1")
                .eq(BHl7YzYaoPlan::getIsDeleted, NO_NUMBER);
        return this.selectList(queryWrapper);
    }

 

    /**
     * 更具groupId，planTime，hospitalNumber集合查出所有用药信息（不包含出入量）
     * @param list
     * @param hospitalNumber
     * @return
     */
    default List<BHl7YzYaoPlan> selectListByGroupIdsAndExeTimeRange(List<HashMap<String,Object>> list, String hospitalNumber) {
        LambdaQueryWrapper<BHl7YzYaoPlan> queryWrapper = new LambdaQueryWrapper<>();
        list.forEach( item ->{
            queryWrapper.or(wapper2->{
                wapper2.eq(BHl7YzYaoPlan::getGroupId , item.get("groupid")).eq(BHl7YzYaoPlan::getPlanTime,item.get("plantime")).eq(BHl7YzYaoPlan::getHospitalNumber,hospitalNumber)
                        .eq(BHl7YzYaoPlan::getIsFromio,NO_NUMBER)
                        .eq(BHl7YzYaoPlan::getIsDeleted, NO_NUMBER);
            });
        });
        queryWrapper.orderByDesc(BHl7YzYaoPlan::getGroupId,BHl7YzYaoPlan::getIsMaster);
        return this.selectList(queryWrapper);
    }

    default List<BHl7YzYaoPlan> selectListByGroupId(String groupId) {
        LambdaQueryWrapper<BHl7YzYaoPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7YzYaoPlan::getGroupId, groupId)
                .eq(BHl7YzYaoPlan::getIsDeleted, NO_NUMBER);
        return this.selectList(queryWrapper);
    }

    /**
     * 根据时间范围,住院号 统计每个人时间段内的用药明细
     *
     * @param startTime      开始时间
     * @param endTime        结束时间
     * @param hospitalNumber 住院号
     * @return
     */
    List<CountExeYaoDetailTimeRangeBO> countDetailTimeRange(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("hospitalNumber") String hospitalNumber,
                                                            @Param("inputType")String inputType);
    default List<BHl7YzYaoPlan> selectSubList(String groupId, String hospitalNumber, String patientIdent, LocalDateTime planTime){
        LambdaQueryWrapper<BHl7YzYaoPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7YzYaoPlan::getGroupId, groupId)
                .eq(BHl7YzYaoPlan::getHospitalNumber, hospitalNumber)
                .eq(BHl7YzYaoPlan::getPatientIdent, patientIdent)
                .eq(BHl7YzYaoPlan::getPlanTime, planTime).orderByDesc(BHl7YzYaoPlan::getIsMaster);
        return selectList(queryWrapper);
    }

    /**
     * 根据分组id和预计划时间查找同一组药物
     *
     * @param groupId  分组id
     * @param planTime 预执行时间
     * @return
     */
    default List<BHl7YzYaoPlan> selectListByGroupIdPlanTime(String groupId, String planTime,String hospitalNumber) {
        LambdaQueryWrapper<BHl7YzYaoPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BHl7YzYaoPlan::getGroupId, groupId)
                .eq(BHl7YzYaoPlan::getPlanTime, planTime)
                .eq(BHl7YzYaoPlan::getHospitalNumber, hospitalNumber)
                .eq(BHl7YzYaoPlan::getIsDeleted, NO_NUMBER);
        return this.selectList(queryWrapper);
    }
}


